<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>
<!DOCTYPE html>
<html>
<@layout.head title="仓库管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div id="searchGroup" class="btn-group" style="position: absolute; ">
            <div style='float:left;margin:5px' class="organizationBox">
                <select class='form-control' id='organization0' onchange="changeOrg(this)">
                    <#list organizationName as item>
                        <option value="${item.id?c}">${item.name}</option>
                    </#list>
                </select>
            </div>
            <div style='float:left;margin:5px'>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                        style="margin-left: 5px;"
                        onclick="javascript:refreshData();">
                    <i class="fa fa-search"></i> 查询
                </button>
            </div>
        </div>
        <!-- 表格 -->
        <div style="position: relative; top:40px; ">
            <@dataTables.commonTable id="mainTable"/>
        </div>
        <!-- 按钮 -->
        <div id="btnGroup" class="btn-group" style="position: absolute; top:40px;">
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:newdetail();">
                <i class="fa fa-plus"></i> 添加
            </button>

            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:editdetail();">
                <i class="fa fa-edit"></i> 修改
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:deldetail();">
                <i class="fa fa-minus"></i> 删除
            </button>

            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:typedetail();">
                <i class="fa fa-list"></i> 资产类型
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:managerdetail();">
                <i class="fa fa-users"></i> 管理员
            </button>
        </div>
    </div>
</div>
<!-- 弹出窗 -->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="600px" >
        <@mform.container id="editForm">
        <input id="id" name="id" type="hidden"/>
        <input id="organizationId" name="organizationId" type="hidden"/>
            <@mform.row>
                <@mform.input_text id='name' name='name' size=6 label='仓库名称' placeholder='仓库名称' icon='fa-edit' />
                <@mform.input_text id='code' name='code' size=6 label='仓库编号' placeholder='仓库编号' icon='fa-edit' />
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='position' name='position' size=12 label='地理位置' placeholder='地理位置' icon='fa-edit' />
            </@mform.row>
        </@mform.container>
    </@modal.editModal>

<!-- 弹出窗-管理员 -->
    <@modal.editModal id="modal-detail2" buttonId="" width="1000px" >
        <@mform.container width="400px">

            <div id="searchGroup2" class="btn-group" style="position: absolute; z-index:2">
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                            style="margin-left: 5px;"
                            onclick="javascript:deleteManager();">
                        <i class="fa fa-minus"></i> 删除选中
                    </button>
            </div>
            <div style="position: relative;  z-index: 1">
                <@dataTables.commonTable id="mainTable1"/>
            </div>
        </@mform.container>
        <@mform.container width="540px">

            <div id="searchGroup3" class="btn-group" style="position: absolute; z-index: 2">
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                            style="margin-left: 5px;"
                            onclick="javascript:addManager();">
                        <i class="fa fa-plus"></i> 加入选中
                    </button>
            </div>
            <div style="position: relative;  z-index: 1">
                <@dataTables.commonTable id="mainTable2"/>
            </div>
        </@mform.container>
    </@modal.editModal>

<!-- 弹出窗-种类 -->
    <@modal.editModal id="modal-detail3" buttonId="" width="1000px" >
        <@mform.container width="400px">

        <div id="searchGroup2" class="btn-group" style="position: absolute; z-index:2">
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                    style="margin-left: 5px;"
                    onclick="javascript:deleteType();">
                <i class="fa fa-minus"></i> 删除选中
            </button>
        </div>
        <div style="position: relative;  z-index: 1">
            <@dataTables.commonTable id="mainTable3"/>
        </div>
        </@mform.container>
        <@mform.container width="540px">

        <div id="searchGroup3" class="btn-group" style="position: absolute; z-index: 2">
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                    style="margin-left: 5px;"
                    onclick="javascript:addType();">
                <i class="fa fa-plus"></i> 加入选中
            </button>
        </div>
        <div style="position: relative;  z-index: 1">
            <@dataTables.commonTable id="mainTable4"/>
        </div>
        </@mform.container>
    </@modal.editModal>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
<script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
<script type="text/javascript">
    var State = {
        <#list state as item>
            "${item.code}": "${item.name}",
        </#list>}
    var columns = [
        {'sWidth': '200px', 'title': 'ID', 'data': 'id'},
        {'sWidth': '200px', 'title': '仓库名称', 'data': 'name'},
        {'sWidth': '100px', 'title': '仓库编号', 'data': 'code'},
        {'sWidth': '300px', 'title': '地理位置', 'data': 'position'},
        {'sWidth': '100px', 'title': '资产类型', 'data': 'eqCount'},
        {'sWidth': '100px', 'title': '管理员', 'data': 'managerCount'},
        {'sWidth': '100px', 'title': '状态', 'data': 'state'},
        {'sWidth': '100px', 'title': '创建时间', 'data': 'createTime'},
        {'sWidth': '100px', 'title': '修改时间', 'data': 'updateTime'}
    ]
    var mainTable = $('#mainTable').winningTable({
        columns: columns,
        usePager: false,
        "paging": false,
        pageLength: 10,
        fixedHeader: false,
        searching: true,
        ordering: true,
        "columnDefs": [{
            "targets": [0],
            "visible": false
        }],
        buttons: [

        ],
        "rowCallback": function (row, data, index) {
            $('td', row).eq(5).html((State[data.state] || "无"));
            $('td', row).eq(3).html(data.eqCount+ "种");
            $('td', row).eq(4).html(data.managerCount+ "人");
        }
    });

    var mainTable1 = $('#mainTable1').winningTable({
        columns: [
            {'sWidth': '200px', 'title': '管理员姓名', 'data': 'staffName'},
            {'sWidth': '200px', 'title': '认证号', 'data': 'telephone'},
            {'sWidth': '200px', 'title': '职务', 'data': 'job'}
        ],
        "paging": false,
        pageLength: 5,
    });

    var mainTable2 = $('#mainTable2').winningTable({
        columns: [
            {'sWidth': '200px', 'title': '所属机构', 'data': 'orgName'},
            {'sWidth': '150px', 'title': '人员姓名', 'data': 'name'},
            {'sWidth': '200px', 'title': '认证号', 'data': 'telephone'},
            {'sWidth': '150px', 'title': '职务', 'data': 'job'},
            {'sWidth': '200px', 'title': '角色', 'data': 'cRoleName'}
        ],
        "paging": true,
        pageLength: 5,
    });

    var mainTable3 = $('#mainTable3').winningTable({
        columns: [
            {'sWidth': '200px', 'title': '类型', 'data': 'typeName'},
            {'sWidth': '200px', 'title': '加入时间', 'data': 'createTime'}
        ],
        "paging": false,
        pageLength: 5,
    });

    var mainTable4 = $('#mainTable4').winningTable({
        columns: [
            {'sWidth': '200px', 'title': '类型', 'data': 'name'},
            {'sWidth': '200px', 'title': '编号', 'data': 'code'}
        ],
        "paging": true,
        pageLength: 5,
    });
//-------------------动态加载各级机构-----------------------------
    function getParentOrg(){
        var parent="0";
        var name="全部一级机构";
        var boxs=$(".organizationBox");
        for(var i=boxs.length-1;i>=0;i--){
            if($(boxs[i]).find("select").val()!=0) {
                parent=$(boxs[i]).find("select").val();
                name= $(boxs[i]).find("option:selected").text();
                break;
            }
        }
        return {
            id:parent,name:name
        }
    }
    // 机构查询
    function refreshOrganization(orgLevel) {
        $.ajax({
            type: 'POST',
            data: {
                datas: JSON.stringify({
                    oneOrganization: getParentOrg().id
                })
            },
            url: "${request.contextPath}/cust/organization/queryList",
            success: function (data) {
                var data=data.datas;
                //成功之后
                var boxs = $(".organizationBox");
                if (boxs.length > 1 && $(boxs[boxs.length - 1]).find("select").val() == 0) {
                    $(boxs[boxs.length - 1]).remove();
                }
                if (orgLevel == null) {
                    orgLevel = $(".organizationBox").length - 1;
                }
                if (data.length > 0 && $("#organization" + orgLevel).val() != 0) {
                    //动态加载下拉框
                    var div = $("<div style='float:left;margin:5px' class='organizationBox'> </div>");
                    var select = $("<select class='form-control' onchange='changeOrg(this)' id='organization" + (orgLevel - 0 + 1) + "')> <option value=0>选择子机构</option> </select>");
                    $("#organization" + orgLevel).parent().after(div.append(select));
                    for (var i = 0; i < data.length; i++) {
                        select.append("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
                    }
                    select.select2();
                }

            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }

    function changeOrg(e) {
        var select = $(e);
        var box = select.parent();
        while (box.next().hasClass("organizationBox")) {
            box.next().remove();
        }
        refreshOrganization();
    }
    refreshData(); refreshOrganization();
//==================================================end======================================================================================
    // 刷新
    function refreshData() {
        mainTable.queryData("${request.contextPath}/base/warehouse/queryList",{
            datas: JSON.stringify({
                organization:getParentOrg().id
            })
        });
    }

    // 新增
    function newdetail() {
        var org=getParentOrg();
        if(org==null||org.id==0){
            swal("错误", "请选择所属机构", "error");
            return;
        }
        $('#modal-detail').find(".modal-title").text('为【'+org.name+'】新增仓库');
        clearForm($('#modal-detail'));
        $("#organizationId").val(org.id);
        $("#btndetailSave").unbind();
        $("#btndetailSave").click(savedetail);
        $('#modal-detail').modal();
    }

    // 修改
    function editdetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var $modal = $("#modal-detail");
        $modal.find(".modal-title").text("修改信息");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm"), data);
        $modal.modal();
        $("#btndetailSave").unbind();
        $("#btndetailSave").click(savedetail);
    }

    // 保存
    function savedetail() {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail form").serializeJSON();
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/base/warehouse/save',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail").modal('hide');
                            refreshData();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 删除
    function deldetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/base/warehouse/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
var selectedWareId;
    // 管理员
    function managerdetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var $modal = $("#modal-detail2");
        $modal.find(".modal-title").text("管理员信息【左侧为已加入的管理员】");
        $modal.modal();
        selectedWareId=datas[0].id;
        mainTable1.queryData("${request.contextPath}/base/warehouse/queryManagerList",{
            datas: JSON.stringify({
                warehouseId:selectedWareId
            })
        })
        mainTable2.queryData("${request.contextPath}/cust/staff/queryList", { datas: JSON.stringify({
            staffInformation:"",
            role:"3"
        })});
    }

    // 加入管理
    function addManager() {
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        var requestData = {
            staffId: data.id,
            warehouseId:selectedWareId
        };
        var params = {datas: JSON.stringify(requestData)};
        $.ajax({
            type: 'POST',
            data: params,
            url: '${request.contextPath}/base/warehouse/saveStaff',
            success: function (data) {
                //成功之后
                if (data.success) {
                    refreshData();
                    mainTable1.queryData("${request.contextPath}/base/warehouse/queryManagerList",{
                        datas: JSON.stringify({
                            warehouseId:selectedWareId
                        })
                    })
                } else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }
    // 删除管理
    function deleteManager() {
        var datas = mainTable1.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        var requestData = {
            id: data.id
        };
        var params = {datas: JSON.stringify(requestData)};
        $.ajax({
            type: 'POST',
            data: params,
            url: '${request.contextPath}/base/warehouse/deleteStaff',
            success: function (data) {
                //成功之后
                if (data.success) {
                    refreshData();
                    mainTable1.queryData("${request.contextPath}/base/warehouse/queryManagerList",{
                        datas: JSON.stringify({
                            warehouseId:selectedWareId
                        })
                    })
                } else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }

    // 管理员
    function typedetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var $modal = $("#modal-detail3");
        $modal.find(".modal-title").text("资产类型信息");
        $modal.modal();
        selectedWareId=datas[0].id;
        mainTable3.queryData("${request.contextPath}/base/warehouse/queryEqClassList",{
            datas: JSON.stringify({
                warehouseId:selectedWareId
            })
        })
        mainTable4.queryData("${request.contextPath}/base/eqClass/queryList", { datas: JSON.stringify({
            superId:"0"
        })});
    }

    // 加入类型
    function addType() {
        var datas = mainTable4.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        var requestData = {
            oneCode: data.code,
            warehouseId:selectedWareId
        };
        var params = {datas: JSON.stringify(requestData)};
        $.ajax({
            type: 'POST',
            data: params,
            url: '${request.contextPath}/base/warehouse/saveType',
            success: function (data) {
                //成功之后
                if (data.success) {
                    refreshData();
                    mainTable3.queryData("${request.contextPath}/base/warehouse/queryEqClassList",{
                        datas: JSON.stringify({
                            warehouseId:selectedWareId
                        })
                    })
                } else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }
    // 删除类型
    function deleteType() {
        var datas = mainTable3.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        var requestData = {
            id: data.id
        };
        var params = {datas: JSON.stringify(requestData)};
        $.ajax({
            type: 'POST',
            data: params,
            url: '${request.contextPath}/base/warehouse/deleteType',
            success: function (data) {
                //成功之后
                if (data.success) {
                    refreshData();
                    mainTable3.queryData("${request.contextPath}/base/warehouse/queryEqClassList",{
                        datas: JSON.stringify({
                            warehouseId:selectedWareId
                        })
                    })
                } else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }
</script>
</@layout.foot>
</body>
</html>